package com.edu.hre.common.writefiles;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreJobCandidateAttachment;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;

/**
 * Servlet implementation class CandidateCV
 */
public class CandidateCV extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CandidateCV() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
		response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
		response.setDateHeader("Expires", 0); // Proxies.
		
		int canId = CommonUtils.parseInt(request.getParameter("canId"), -1); 
		HreJobCandidateAttachment attach = baseService.getFirstPojo("candidateId", Compare.EQUAL, canId, null, null, 0, HreJobCandidateAttachment.class);
		if(attach != null && attach.getFileContent() != null){
			response.setContentLength(attach.getFileContent().length);
			response.setContentType(attach.getFileType());
			response.getOutputStream().write(attach.getFileContent());
		} else {
			response.getWriter().write("CV file is NOT available!");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
